<template>
	<view class="container">
		<view class="form-container">
			<!-- 标题 -->
			<view class="title">修改个人信息</view>

			<!-- 用户名 -->
			<view class="form-item">
				<u-icon name="account" size="24" color="#409EFF" class="icon"></u-icon>
				<view class="label">用户名：</view>
				<u-input v-model="userInfo.name" placeholder="请输入用户名" class="input" />
			</view>

			<!-- 性别 -->
			<view class="form-item">
				<u-icon name="man" size="24" color="#FF69B4" class="icon"></u-icon>
				<view class="label">性别：</view>
				<u-radio-group v-model="userInfo.gender" class="radio-group">
					<u-radio name="male" label="男" class="radio"></u-radio>
					<u-radio name="female" label="女" class="radio"></u-radio>
				</u-radio-group>
			</view>

			<!-- 年龄 -->
			<view class="form-item">
				<u-icon name="calendar" size="24" color="#FFA500" class="icon"></u-icon>
				<view class="label">年龄：</view>
				<u-input v-model="userInfo.age" type="number" placeholder="请输入年龄" class="input" />
			</view>

			<!-- 体重 -->
			<view class="form-item">
				<u-icon name="heart" size="24" color="#55aa00" class="icon"></u-icon>
				<view class="label">体重：</view>
				<u-input v-model="userInfo.weight" type="number" placeholder="请输入体重 (kg)" class="input" />
			</view>

			<!-- 身高 -->
			<view class="form-item">
				<u-icon name="coupon" size="24" color="#aa557f" class="icon"></u-icon>
				<view class="label">身高：</view>
				<u-input v-model="userInfo.height" type="number" placeholder="请输入身高 (cm)" class="input" />
			</view>

			<!-- 提交按钮 -->
			<view class="button-container">
				<u-button type="primary" shape="circle" @click="submitUserInfoChange">提交</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					name: "",
					gender: "male",
					age: "",
					weight: "",
					height: "",
				},
			};
		},
		async onLoad() {
			await this.loadUserInfo();
		},
		methods: {
			async loadUserInfo() {
				await uni.request({
					url: `${this.$baseUrl}/user/getMessage`,
					method: "POST",
					header: {
						Authorization: "Bearer " + uni.getStorageSync("access_token"),
					},
				}).then((data) => {
					var [err, res] = data;
					if (res.data.data) {
						const userData = res.data.data[0];
						this.userInfo = {
							name: userData.name ,
							gender: userData.gender,
							age: userData.age,
							weight: userData.weight,
							height: userData.height,
						};
					}
				});		
			},
			async submitUserInfoChange() {
				if (!this.userInfo.name || !this.userInfo.age || !this.userInfo.weight || !this.userInfo.height) {
					uni.showToast({
						title: "请填写完整信息",
						icon: "none",
					});
					return;
				}

				await uni.request({
					url: `${this.$baseUrl}/user/updateMessage`,
					method: "POST",
					header: {
						Authorization: "Bearer " + uni.getStorageSync("access_token"),
					},
					data: this.userInfo,
				}).then((data) => {
					var [err, res] = data;
					if (res.data.updateSucceed) {
						uni.showToast({
							title: "个人信息修改成功",
							icon: "success",
							duration: 1500, // 提示显示时长
							success: () => {
								// 延迟跳转，确保用户看到提示
								setTimeout(() => {
									uni.navigateBack(); // 返回上一页
								}, 1500); // 延迟时间与提示显示时长一致
							},
						});
					} else {
						uni.showToast({
							title: "个人信息修改失败",
							icon: "none",
						});
					}
				});	
			},
		},
	};
</script>

<style lang="scss">
	.container {
		padding: 40rpx;
		background: linear-gradient(135deg, #f5f7fa, #c3cfe2);
		min-height: 100vh;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.form-container {
		background-color: #fff;
		border-radius: 24rpx;
		padding: 60rpx 40rpx;
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
		width: 100%;
		max-width: 600rpx;
	}

	.title {
		font-size: 40rpx;
		font-weight: bold;
		color: #333;
		text-align: center;
		margin-bottom: 60rpx;
	}

	.form-item {
		display: flex;
		align-items: center;
		margin-bottom: 40rpx;
		border-bottom: 2rpx solid #eee;
		padding-bottom: 20rpx;

		.icon {
			margin-right: 20rpx;
		}

		.input {
			flex: 1;
			font-size: 32rpx;
		}

		.radio-group {
			display: flex;
			align-items: center;

			.radio {
				margin-right: 40rpx;
			}
		}
	}

	.button-container {
		margin-top: 60rpx;

		.u-button {
			background: linear-gradient(135deg, #409EFF, #67C23A);
			color: #fff;
			font-size: 32rpx;
			height: 80rpx;
			line-height: 80rpx;
		}
	}
</style>